using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;

namespace IGGCore
{
    public class PlayersParser : BaseParser
    {

        /// <summary>
        /// Parse the players file related to the specified game
        /// </summary>
        public void ParsePlayers(DateTime currentDate, Game game)
        {
            string id = game.Id.Replace("/", "_");
            id = id.Replace("-", "_");
            string url = CoreConstants.MLB_ONLINE_BASE_URL
                + "/" + ToolBox.GetDateURL(currentDate)
                + "/gid_" + id + "/" + CoreConstants.PLAYERS_URL;
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load(url);
                XmlNode gameNode = xmldoc.SelectSingleNode("game");
                XmlNodeList teamNodelist = gameNode.SelectNodes("team");

                foreach (XmlNode teamNode in teamNodelist)
                {
                    string type = GetAttribute(teamNode, "type", "UNK");
                    Team team = game.AwayTeam;
                    if (type.Equals("home"))
                    {
                        team = game.HomeTeam;
                    }
                    XmlNodeList playerList = teamNode.SelectNodes("player");
                    foreach (XmlNode playerNode in playerList)
                    {
                        Player player = ParsePlayer(playerNode);
                        team.Players[player.Id] = player;
                    }
                }
            }
            catch (WebException)
            {
                xmldoc = null;
            }
            finally
            {
                xmldoc = null;
            }
        }

        /// <summary>
        /// Parse a player
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public Player ParsePlayer(XmlNode node)
        {
            if (node != null)
            {
                Player player = new Player();
                player.Id = GetAttribute(node, "id", "NO_ID");
                player.Ab = GetAttribute(node, "ab", -1);
                player.ERA = GetAttribute(node, "era", null);
                player.AVG = GetAttribute(node, "avg", null);
                player.EarnedRuns = GetAttribute(node, "er", -1);
                player.FirstName = GetAttribute(node, "first", "MR.");
                player.LastName = GetAttribute(node, "last", "NO_NAME");
                player.Hits = GetAttribute(node, "h", -1);
                player.Homeruns = GetAttribute(node, "hr", -1);
                player.IP = GetAttribute(node, "ip", null);
                player.Position = GetAttribute(node, "position", null);
                player.RL = GetAttribute(node, "rl", null);
                player.Losses = GetAttribute(node, "losses", -1);
                player.Number = GetAttribute(node, "number", "NO_NUM");
                player.RBI = GetAttribute(node, "rbi", -1);
                player.Report = GetAttribute(node, "report", null);
                player.Saves = GetAttribute(node, "saves", -1);
                player.StrikeOuts = GetAttribute(node, "so", -1);
                player.ThrowingHand = GetAttribute(node, "throwinghand", null);
                player.Wins = GetAttribute(node, "wins", -1);
                return player;
            }
            else
            {
                return null;
            }
        }
    }
}
